<?php
class DataTableRendererHTMLList extends DataTableRenderer
{

	private function printYear($date)
	{
		return "<td class=\"EventReportYear\" colspan=\"4\">".$date->format("Y")."</td></tr><tr>";
	}

	private function printMonth($date)
	{
		return "<td class=\"EventReportMonth\" colspan=\"4\">".$date->format("F")."</td></tr><tr>";
	}

	private function printDay($date)
	{
		return "<td class=\"EventReportDay\" colspan=\"4\">".$date->format("j l")."</td></tr><tr>";
	}

	public function getHTML()
	{
		$html = "<SCRIPT language='javascript'>\n";
		$html .= "function ".$this->dataTable->getName()."Refresh()\n";
		$html .= "{\n";
		$html .= "document.getElementById(\"Action\").value = \"EventReport\";\n";
		$html .= "document.getElementById(\"PageForm\").submit();\n";
		$html .= "}\n";
		$html .= "</SCRIPT>\n";


		$RecordID = new Hidden();
		$RecordID->setName("RecordID");
		$html .= $RecordID->getHTML();

		$DBTableName = new Hidden();
		$DBTableName->setName("DBTableName");
		$html .= $DBTableName->getHTML();

		$html .= "<table>";

		$this->dataTable->getDatabase()->ExecuteSQL($this->dataTable->getSQLCommand());
//		echo $this->dataTable->getSQLCommand();


		$previousDate = new DateTime("-10 years");
		while($row=$this->dataTable->getDatabase()->getRow())
		{
			echo mysql_error();

			$html .= "<tr>";
			foreach($this->dataTable->getColumns() as $column)
			{
//				$DeletedFontText = ($row["DeletedBy"]=='1')?" style=\"color: #dd0000\"":"";

				switch ($column->getDisplayAs())
				{
					case 'DateOnly':
						$date = new DateTime($row[$column->getField()]);
						if($date->format("Y")!=$previousDate->format("Y"))$html.=$this->printYear($date);
						if($date->format("Ym")!=$previousDate->format("Ym"))$html.=$this->printMonth($date);
						if($date->format("Ymd")!=$previousDate->format("Ymd"))$html.=$this->printDay($date);
						$previousDate = $date;
						$html .= "<td width=\"20px\"></td>";
//						$html .= "<td>".$date->format("D d M Y")."</td>";
						break;

					case 'TimeOnly':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td>".$date->format("H:i")."</td>";
						break;

					case 'DateTime':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td>".$date->format("d M Y  H:i")."</td>";
						break;

					case 'SingleEnumeration':
						$field = $column->getField();
						$sql = "select ".substr($field,0,-2)." FROM ".substr($field,0,-2)."s WHERE ".$field."=".$row[$field];
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$row1 = $database->getRow();
						$html .= "<td>".$row1[substr($field,0,-2)]."</td>";
						break;

					case 'MultiEnumeration':
						$field = $column->getField();
						$sql = "SELECT id.".$field."NameID, n.ShortName FROM ".$field."s as id INNER JOIN ".$field."Names as n ON id.".$field."NameID = n.".$field."NameID WHERE id.EventID = ".$row['ColumnID'];
//						echo $sql;
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$result = "";
						while($row1 = $database->getRow())
					{
							$result.=$row1["ShortName"]."&nbsp;";
//							print_r ($row1);
					}
						$html .= "<td>".$result."</td>";
						break;

					default:
						$html .= "<td>".$row[$column->getField()]."</td>";
						break;	
				}
			}

			$html .= "</tr>\n";

		}


		$html .= "</table>";

		$html .= "<p>";

		$html .= "</p>";

		return $html;


	}

}

?>